package com.wandong.cinema.service.impl;

import com.wandong.cinema.entity.Hall;
import com.wandong.cinema.service.HallService;
import com.wandong.cinema.util.IdGenerator;
import com.wandong.cinema.util.InputUtil;
import com.wandong.cinema.util.LogUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class HallServiceImpl implements HallService {
    // 存储所有放映厅，放映厅ID作为键
    private static final Map<String, Hall> HALL_MAP = new ConcurrentHashMap<>();
    
    // 初始化默认放映厅
    static {
        HALL_MAP.put("H001", new Hall("H001", "黄金厅", 8, 10, "黄金厅"));
        HALL_MAP.put("H002", new Hall("H002", "白银厅", 10, 12, "白银厅"));
        HALL_MAP.put("H003", new Hall("H003", "翠厅", 7, 8, "翠厅"));
        HALL_MAP.put("H004", new Hall("H004", "钻石厅", 7, 8, "钻石厅"));
        HALL_MAP.put("H005", new Hall("H005", "宝石厅", 8, 9, "宝石厅"));
    }


    @Override
    public void addHall() {
        try {
            System.out.println("\n=== 添加放映厅 ===");
            System.out.print("请输入放映厅名称: ");
            String name = InputUtil.inputString();
            System.out.print("请输入行数: ");
            int rowCount = InputUtil.inputInteger();
            System.out.print("请输入列数: ");
            int colCount = InputUtil.inputInteger();
            System.out.print("请输入类型(普通厅/IMAX厅/VIP厅等): ");
            String type = InputUtil.inputString();
            
            if (rowCount <= 0 || colCount <= 0) {
                System.out.println("行数和列数必须为正数");
                return;
            }
            
            String hallId = IdGenerator.generateHallId();
            Hall hall = new Hall(hallId, name, rowCount, colCount, type);
            
            HALL_MAP.put(hallId, hall);
            LogUtil.log("添加放映厅：" + name + ", ID: " + hallId);
            System.out.println("放映厅添加成功，放映厅ID: " + hallId);
        } catch (Exception e) {
            System.out.println("添加放映厅失败: " + e.getMessage());
        }
    }

    @Override
    public void deleteHall() {
        System.out.println("\n=== 删除放映厅 ===");
        System.out.print("请输入要删除的放映厅ID: ");
        String hallId = InputUtil.inputString();
        
        if (!HALL_MAP.containsKey(hallId)) {
            System.out.println("放映厅不存在");
            return;
        }
        
        // 实际实现中应先检查是否有排片，有排片则不能删除
        Hall hall = HALL_MAP.remove(hallId);
        LogUtil.log("删除放映厅：" + hall.getName() + ", ID: " + hallId);
        System.out.println("放映厅删除成功");
    }

    @Override
    public void updateHall() {
        System.out.println("\n=== 修改放映厅 ===");
        System.out.print("请输入要修改的放映厅ID: ");
        String hallId = InputUtil.inputString();
        
        if (!HALL_MAP.containsKey(hallId)) {
            System.out.println("放映厅不存在");
            return;
        }
        
        Hall hall = HALL_MAP.get(hallId);
        System.out.println("当前放映厅信息: " + hall);
        
        System.out.print("请输入新名称(回车不修改): ");
        String newName = InputUtil.inputString();
        if (!newName.isEmpty()) {
            hall.setName(newName);
        }
        
        System.out.print("请输入新类型(回车不修改): ");
        String newType = InputUtil.inputString();
        if (!newType.isEmpty()) {
            hall.setType(newType);
        }
        
        // 不允许修改座位数，因为可能已有排片和售票记录
        
        HALL_MAP.put(hallId, hall);
        LogUtil.log("修改放映厅：" + hall.getName() + ", ID: " + hallId);
        System.out.println("放映厅修改成功");
    }

    @Override
    public void queryHalls() {
        System.out.println("\n=== 放映厅查询 ===");
        
        if (HALL_MAP.isEmpty()) {
            System.out.println("没有放映厅信息");
            return;
        }
        
        System.out.println("查询结果:");
        for (Hall hall : HALL_MAP.values()) {
            System.out.println(hall);
        }
    }

    @Override
    public Hall getHallById(String hallId) {
        return HALL_MAP.get(hallId);
    }
}
